/*  static char SccsId[] = "@(#)heli.c	1.6	9/19/95";
 *         Copyright (c) KUBOTA Corporation and HUDSON SOFT,
 *                  1994, 1995 All Rights Reserved
 *
 *  This software comprises unpublished confidential information of
 *  KUBOTA Corp. and HUDSON SOFT. It may not be used, copied or made
 *  available to anyone, except in accordance with the license under
 *  which it is furnished.
 */

#include <farl.h>

#if !defined( NULL )
#define	NULL	(0)
#endif

#define	BODY_NUM	(214)
#define	CANOPY_NUM	(16)
#define	ENGINE_NUM	(18)
#define	NOZZLE_NUM	(18)
#define	ROTOR_NUM	(24)

#include "body.h"
#include "canopy.h"
#include "engine.h"
#include "nozzle.h"
#include "rotor.h"

float dir[3] = { 0.433012, 0.75, 0.5 };

void
main()
{
	short		vert[768][3];
	short		fnor[256][3];
	unsigned short	fcol[256];
	unsigned short	body_buf[4096];
	unsigned short	canopy_buf[512];
	unsigned short	engine_buf[512];
	unsigned short	nozzle_buf[512];
	unsigned short	rotor_buf[512];
	int		body_cnt, canopy_cnt, engine_cnt;
	int		nozzle_cnt, rotor_cnt;
	int		i, cnt;

	FX73init();
	FX73_cmap_i4c5();
	FarlInit();
	FarlSetMemReg( FARL_INTERRUPT_MASK_REG, FARL_INTERRUPT_AFL );
	FarlSetTEControlReg( FARL_TE_ICM | FARL_TE_BCE | FARL_TE_LTEN );
	FarlSetPEICMaskReg( FARL_ICMASK_I4C5 );
	FarlSetLightCoef( FarlFix1015( 0.3 ), FarlFix1015( 0.9 ), 0,
						FarlFix1015( 1.0 ), 0 );
	FarlSetLight( FARL_SET_LIGHT1, FarlFix1015( 0.433012 ),
			FarlFix1015( 0.75 ), FarlFix1015( 0.5 ) );
	FarlSetPEClearColorReg( 0x807 );

	/*	body		*/
	for ( i = 0; i < BODY_NUM; i++ ) {
		vert[i*3+0][0]	= FarlFix1015( body_vert[i*3+0][0] );
		vert[i*3+0][1]	= FarlFix1015( body_vert[i*3+0][1] );
		vert[i*3+0][2]	= FarlFix1015( body_vert[i*3+0][2] );

		vert[i*3+1][0]	= FarlFix1015( body_vert[i*3+1][0] );
		vert[i*3+1][1]	= FarlFix1015( body_vert[i*3+1][1] );
		vert[i*3+1][2]	= FarlFix1015( body_vert[i*3+1][2] );

		vert[i*3+2][0]	= FarlFix1015( body_vert[i*3+2][0] );
		vert[i*3+2][1]	= FarlFix1015( body_vert[i*3+2][1] );
		vert[i*3+2][2]	= FarlFix1015( body_vert[i*3+2][2] );

		fnor[i][0]	= FarlFix1015( body_fnor[i][0] );
		fnor[i][1]	= FarlFix1015( body_fnor[i][1] );
		fnor[i][2]	= FarlFix1015( body_fnor[i][2] );

		fcol[i]		=  6 | ( (int)( 0.5 * ( 1 << 6 ) ) << 6 );
	}
	body_cnt = FarlDumpTriangleList( FARL_GTYPE_FCFN,
			BODY_NUM, (unsigned short *)vert, (unsigned short *)fnor, fcol, NULL, body_buf );

	/*	canopy		*/
	for ( i = 0; i < CANOPY_NUM; i++ ) {
		vert[i*3+0][0]	= FarlFix1015( canopy_vert[i*3+0][0] );
		vert[i*3+0][1]	= FarlFix1015( canopy_vert[i*3+0][1] );
		vert[i*3+0][2]	= FarlFix1015( canopy_vert[i*3+0][2] );

		vert[i*3+1][0]	= FarlFix1015( canopy_vert[i*3+1][0] );
		vert[i*3+1][1]	= FarlFix1015( canopy_vert[i*3+1][1] );
		vert[i*3+1][2]	= FarlFix1015( canopy_vert[i*3+1][2] );

		vert[i*3+2][0]	= FarlFix1015( canopy_vert[i*3+2][0] );
		vert[i*3+2][1]	= FarlFix1015( canopy_vert[i*3+2][1] );
		vert[i*3+2][2]	= FarlFix1015( canopy_vert[i*3+2][2] );

		fnor[i][0]	= FarlFix1015( canopy_fnor[i][0] );
		fnor[i][1]	= FarlFix1015( canopy_fnor[i][1] );
		fnor[i][2]	= FarlFix1015( canopy_fnor[i][2] );

		fcol[i]		=  4 | ( (int)( 0.7 * ( 1 << 6 ) ) << 6 );
	}
	canopy_cnt = FarlDumpTriangleList( FARL_GTYPE_FCFN, CANOPY_NUM,
			(unsigned short *)vert, (unsigned short *)fnor,
						fcol, NULL, canopy_buf );

	/*	engine		*/
	for ( i = 0; i < ENGINE_NUM; i++ ) {
		vert[i*3+0][0]	= FarlFix1015( engine_vert[i*3+0][0] );
		vert[i*3+0][1]	= FarlFix1015( engine_vert[i*3+0][1] );
		vert[i*3+0][2]	= FarlFix1015( engine_vert[i*3+0][2] );

		vert[i*3+1][0]	= FarlFix1015( engine_vert[i*3+1][0] );
		vert[i*3+1][1]	= FarlFix1015( engine_vert[i*3+1][1] );
		vert[i*3+1][2]	= FarlFix1015( engine_vert[i*3+1][2] );

		vert[i*3+2][0]	= FarlFix1015( engine_vert[i*3+2][0] );
		vert[i*3+2][1]	= FarlFix1015( engine_vert[i*3+2][1] );
		vert[i*3+2][2]	= FarlFix1015( engine_vert[i*3+2][2] );

		fnor[i][0]	= FarlFix1015( engine_fnor[i][0] );
		fnor[i][1]	= FarlFix1015( engine_fnor[i][1] );
		fnor[i][2]	= FarlFix1015( engine_fnor[i][2] );

		fcol[i]		= 11 | ( (int)( 0.7 * ( 1 << 6 ) ) << 6 );
	}
	engine_cnt = FarlDumpTriangleList( FARL_GTYPE_FCFN, ENGINE_NUM,
			(unsigned short *)vert, (unsigned short *)fnor,
						fcol, NULL, engine_buf );

	/*	nozzle		*/
	for ( i = 0; i < NOZZLE_NUM; i++ ) {
		vert[i*3+0][0]	= FarlFix1015( nozzle_vert[i*3+0][0] );
		vert[i*3+0][1]	= FarlFix1015( nozzle_vert[i*3+0][1] );
		vert[i*3+0][2]	= FarlFix1015( nozzle_vert[i*3+0][2] );

		vert[i*3+1][0]	= FarlFix1015( nozzle_vert[i*3+1][0] );
		vert[i*3+1][1]	= FarlFix1015( nozzle_vert[i*3+1][1] );
		vert[i*3+1][2]	= FarlFix1015( nozzle_vert[i*3+1][2] );

		vert[i*3+2][0]	= FarlFix1015( nozzle_vert[i*3+2][0] );
		vert[i*3+2][1]	= FarlFix1015( nozzle_vert[i*3+2][1] );
		vert[i*3+2][2]	= FarlFix1015( nozzle_vert[i*3+2][2] );

		fnor[i][0]	= FarlFix1015( nozzle_fnor[i][0] );
		fnor[i][1]	= FarlFix1015( nozzle_fnor[i][1] );
		fnor[i][2]	= FarlFix1015( nozzle_fnor[i][2] );

		fcol[i]		=  7 | ( (int)( 0.6 * ( 1 << 6 ) ) << 6 );
	}
	nozzle_cnt = FarlDumpTriangleList( FARL_GTYPE_FCFN, NOZZLE_NUM,
			(unsigned short *)vert, (unsigned short *)fnor,
						fcol, NULL, nozzle_buf );

	/*	rotor		*/
	for ( i = 0; i < ROTOR_NUM; i++ ) {
		vert[i*3+0][0]	= FarlFix1015( rotor_vert[i*3+0][0] );
		vert[i*3+0][1]	= FarlFix1015( rotor_vert[i*3+0][1] );
		vert[i*3+0][2]	= FarlFix1015( rotor_vert[i*3+0][2] );

		vert[i*3+1][0]	= FarlFix1015( rotor_vert[i*3+1][0] );
		vert[i*3+1][1]	= FarlFix1015( rotor_vert[i*3+1][1] );
		vert[i*3+1][2]	= FarlFix1015( rotor_vert[i*3+1][2] );

		vert[i*3+2][0]	= FarlFix1015( rotor_vert[i*3+2][0] );
		vert[i*3+2][1]	= FarlFix1015( rotor_vert[i*3+2][1] );
		vert[i*3+2][2]	= FarlFix1015( rotor_vert[i*3+2][2] );

		fnor[i][0]	= FarlFix1015( rotor_fnor[i][0] );
		fnor[i][1]	= FarlFix1015( rotor_fnor[i][1] );
		fnor[i][2]	= FarlFix1015( rotor_fnor[i][2] );

		fcol[i]		= 30 | ( (int)( 0.5 * ( 1 << 6 ) ) << 6 );
	}
	rotor_cnt = FarlDumpTriangleList( FARL_GTYPE_FCFN, ROTOR_NUM,
			(unsigned short *)vert, (unsigned short *)fnor,
						fcol, NULL, rotor_buf );

	for ( cnt = 0 ;; cnt += 10 ) {
		FarlSetUnitMatrix187();
		FarlRotateY4x4M187( cnt % 3600 );
		FarlCopyDstToSrc();
		FarlTranslate4x4M187( 0x0000, 0x0000, 0xff80 );
		FarlCopyDstToSrc();
		FarlPerseZ4x4M187( 0xff80 );
		FarlCopyDstToObj();

		FarlSetUnitMatrix1015();
		FarlRotateY3x3M1015( cnt % 3600 );
		FarlCopyDstToNrm();

		FarlWriteFifoHA( body_buf, body_cnt );
		FarlWriteFifoHA( canopy_buf, canopy_cnt );
		FarlWriteFifoHA( engine_buf, engine_cnt );
		FarlWriteFifoHA( nozzle_buf, nozzle_cnt );

		FarlSetUnitMatrix187();
		FarlRotateY4x4M187( ( cnt * 10 ) % 3600 );
		FarlCopyDstToSrc();
		FarlTranslate4x4M187( 0x0000, 0x0000, 0xff80 );
		FarlCopyDstToSrc();
		FarlPerseZ4x4M187( 0xff80 );
		FarlCopyDstToObj();

		FarlSetUnitMatrix1015();
		FarlRotateY3x3M1015( ( cnt * 10 ) % 3600 );
		FarlCopyDstToNrm();

		FarlWriteFifoHA(rotor_buf, rotor_cnt);

		FarlSwapWindow();
	}
}
